package controle.turma;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import controle.Curso.cursoDAO;
import controle.professor.professorDAO;

import modelagem.Turma;

import visao.turma.BuscadeTurma;
import visao.turma.interfaceTurma;

public class AcoesInterfaceTurma implements ActionListener {
	private interfaceTurma it;
	private turmaDAO bd;

	public AcoesInterfaceTurma() {
		
	}
	
	public AcoesInterfaceTurma(interfaceTurma it) {
		this.it = it;
		bd = new turmaDAO();
	}

	
	public void actionPerformed(ActionEvent evt) {
		if(evt.getSource() == it.getBtnCancelar()){
			it.dispose();
		}
		if(evt.getSource() == it.getBtnAdd()){
			it.getModeloTabAlunDaTurma().add(it.getModeloTabela().getValue(it.getTableAlunMat().getSelectedRow()));
			it.getTableAlunNaTurma().repaint();
		}
		if(evt.getSource() == it.getBtnSalvar()){
			Turma t = new Turma();
			t.setId_prof(new professorDAO().buscar((String) it.getComboBoxProf().getSelectedItem()).get(0).getId());
			t.setId_curso(new cursoDAO().buscar((String) it.getComboBoxCurso().getSelectedItem()).get(0).getId());
			t.setCodigo(it.getTextCodigo().getText());
			int[] aluns = new int[it.getModeloTabAlunDaTurma().getRowCount()];
			for(int i = 0; i < it.getModeloTabAlunDaTurma().getRowCount(); i++){
				aluns[i] = it.getModeloTabAlunDaTurma().getValue(i).getId();
			}
			t.setAlunos(aluns);
			
			bd.inserir(t);
			
		}
		if(evt.getSource() == it.getBtnVisualizarTurmas()){
			BuscadeTurma bt = new BuscadeTurma(it);
			bt.setVisible(true);
		}
			
	}

}
